/**	
 * <br>
 * Copyright 2011 IFlyTek. All rights reserved.<br>
 * <br>			 
 * Package: com.iflytek.gwrq.gwrqtj <br>
 * FileName: AnalyseHqlMaker.java <br>
 * <br>
 * @version
 * @author hyzha@iflytek.com
 * @created 2013-5-16
 * @last Modified 
 * @history
 */

package com.iflytek.ci.common;

import com.iflytek.iframework.utils.HqlMaker;
import com.iflytek.iframework.utils.StringUtils;

/**
 * 分析HQL构造类
 * 
 * @author hyzha@iflytek.com
 * @lastModified
 * @history
 */

public class AnalyseHqlMaker {

    /**
     * 查询下级SQL
     * 
     * @param col
     *            字段名称
     * @param queryCondition
     *            查询条件
     * @return
     * @author hyzha@iflytek.com
     * @created 2013-5-17 上午11:55:40
     * @lastModified
     * @history
     */
    public static String selectSubSql(String col, String queryCondition) {
        if (StringUtils.isBlank(queryCondition) || StringUtils.isBlank(col)) {
            return StringUtils.EMPTY;
        }
        StringBuilder where = new StringBuilder();
        where.append(" and ").append(col).append(" ");
        int len = queryCondition.length();
        // 末尾循环截取'00'
        String gwdq_ = StringUtils.toNewsOrgCode(queryCondition);
        if (gwdq_.length() != len) {
            where.append("like '").append(gwdq_).append("%' ");
        } else {
            where.append("='").append(queryCondition).append("'");
        }
        return where.toString();
    }

    /**
     * 查询时间
     * 
     * @param col
     *            字段名称
     * @param kssj
     *            开始时间
     * @param jssj
     *            结束时间
     * @return
     * @author hyzha@iflytek.com
     * @created 2013-5-17 下午12:02:38
     * @lastModified
     * @history
     */
    public static String selectStringDateSql(String col, String kssj,
            String jssj) {
        if (StringUtils.isBlank(col)) {
            return StringUtils.EMPTY;
        }
        StringBuilder where = new StringBuilder();
        where.append(HqlMaker.popuBeginStringDateHql(col, kssj));
        where.append(HqlMaker.popuEndStringDateHql(col, jssj));
        return where.toString();
    }

    /**
     * 查询相等
     * 
     * @param col
     *            字段名称
     * @param parent
     *            查询条件
     * @return
     * @author hyzha@iflytek.com
     * @created 2013-5-17 下午02:04:46
     * @lastModified
     * @history
     */
    public static String selectEqSql(String col, String queryCondition) {
        if (StringUtils.isBlank(col)) {
            return StringUtils.EMPTY;
        }
        StringBuilder where = new StringBuilder();
        where.append(HqlMaker.popuHqlEq(col, queryCondition));
        return where.toString();
    }

    /**
     * 大于等于运算
     * 
     * @param col
     *            字段名称
     * @param kssj
     *            开始时间
     * @return
     * @author xmzhu2
     * @created 2013-5-30 下午4:10:30
     * @lastModified
     * @history
     */
    public static String popuGtE(String col, String kssj) {
        if (StringUtils.isBlank(col)) {
            return StringUtils.EMPTY;
        }
        return " and " + col + ">='" + kssj + "'";
    }

    /**
     * 小于等于运算
     * 
     * @param col
     *            字段名称
     * @param kssj
     *            开始时间
     * @return
     * @author xmzhu2
     * @created 2013-5-30 下午4:10:30
     * @lastModified
     * @history
     */
    public static String popuLtE(String col, String jssj) {
        if (StringUtils.isBlank(col)) {
            return StringUtils.EMPTY;
        }
        return " and " + col + "<='" + jssj + "'";
    }

    /**
     * 
     * 空运算
     * 
     * @param param
     *            字段名称
     * @return
     * @author sbwang@iflytek.com
     * @created 2013-6-26 上午09:27:57
     * @lastModified
     * @history
     */
    public static String popuNull(String param) {
        return " and " + param + " is null ";
    }

    /**
     * 
     * 非空运算
     * 
     * @param column
     *            字段名称
     * @return
     * @author sbwang@iflytek.com
     * @created 2013-6-26 上午09:29:13
     * @lastModified
     * @history
     */
    public static String popuNotNull(String column) {
        return " and " + column + " is not null ";
    }

    /**
     * 组装like hql (处理特殊字符的sql脚本注入问题)
     * 
     * @param column
     *            列名
     * @param param
     *            参数
     * @return
     */
    public static String popuHqlLikeForEscape(String column, String param) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(param)) {
            sb.append(" and ").append(column).append(" like '%").append(
                    param.trim()).append("%'  escape '\\' ");
        }
        return sb.toString();
    }

    /**
     * 
     * 排序(单个字段排序)
     * 
     * @param column
     *            字段名称
     * @param sort
     *            排序类型（升序或者降序）
     * @return
     * @author sbwang@iflytek.com
     * @created 2013-6-26 下午04:17:54
     * @lastModified
     * @history
     */
    public static String popuHqlSortOne(String column, String sort) {
        return " order by " + column + " " + sort;
    }

    /**
     * 
     * 排序(两个字段排序)
     * 
     * @param column1
     *            字段名称1
     * @param sort1
     *            排序类型1（升序或者降序）
     * @param column2
     *            字段名称2
     * @param sort2
     *            排序类型2（升序或者降序）
     * @return
     * @author sbwang@iflytek.com
     * @created 2013-6-26 下午04:17:54
     * @lastModified
     * @history
     */
    public static String popuHqlSortTwo(String column1, String sort1,
            String column2, String sort2) {
        return " order by " + column1 + " " + sort1 + ", " + column2 + " "
                + sort2;
    }

    /**
     * 
     * 排序(三个字段排序)
     * 
     * @param column1
     *            字段名称1
     * @param sort1
     *            排序类型1（升序或者降序）
     * @param column2
     *            字段名称2
     * @param sort2
     *            排序类型2（升序或者降序）
     * @param sort3
     *            排序类型3（升序或者降序）
     * @param column3
     *            字段名称3
     * 
     * @return
     * @author sbwang@iflytek.com
     * @created 2013-6-26 下午04:17:54
     * @lastModified
     * @history
     */
    public static String popuHqlSortThree(String column1, String sort1,
            String column2, String sort2, String column3, String sort3) {
        return " order by " + column1 + " " + sort1 + ", " + column2 + " "
                + sort2 + ", " + column3 + " " + sort3;
    }

    /**
     * 
     * hql 中 from语句
     * 
     * @param tableName
     *            表名（物理表映射的PO对象名）
     * @return
     * @author sbwang@iflytek.com
     * @created 2013-6-26 下午04:40:09
     * @lastModified
     * @history
     */
    public static String popuHqlFrom(String tableName) {
        return "from " + tableName + " where 1=1 ";
    }
}
